//
//  ST_TIANDelayTaskComponent.swift
//  btvideo
//
//  Created by Jason on 2021/11/2.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import Foundation
import SwiftyUserDefaults


class ST_TIANDelayTaskComponent {
    
    fileprivate var current_say_hi_id:String?
    
    fileprivate var requested_auto_chat_gift_set = Set<String>()
    
    func runScript(afterDelay:TimeInterval){
        
        DispatchQueue.main.asyncAfter(deadline: .now() + afterDelay, execute: {
            
            self.fetch_say_hi_handle()
            
            self.request_auto_chat_gift_handle()
            
        })
    }
    
    fileprivate func fetch_say_hi_handle(){
        
//        男 登录 成功 不是在调接口中
        guard let user = ST_BTUserInfoManager.shared.current_account_info, user.userinfo?.sex == .male && user.token.count > 1, current_say_hi_id != user.accid else{
            
            return
        }
        
        current_say_hi_id = user.accid
        
        
        let ids = ST_ConversationAdapter.shared.sessionList.map({$0.sessionId}).joined(separator: ",")
        
        JVrequest(PH.sendAutoSayHiMsgApi,parameters: ["accids":ids]) { (json) in
            
        } failure: { (code, err) in
            
        }

        
    }
    
    fileprivate func request_auto_chat_gift_handle(){
        
        guard let user = ST_BTUserInfoManager.shared.current_account_info, user.userinfo?.sex == .female && user.token.count > 1 else{
            return
        }
        
        let key = user.accid + "_" + "femaleAutoRequestGift_July"
        
        if requested_auto_chat_gift_set.contains(key){
            return
        }
        
        requested_auto_chat_gift_set.update(with: key)
        
        var times = 0
        var isInToday = false
//        value 规则 = {  accid + "@" + Date().unixTimestamp.int.string + "@" + 次数 }
        if let value = Defaults.string(forKey: key){
            
            let array = value.components(separatedBy: "@")
            
            times = array[safe: 2]?.int ?? 0
            
            if let dateTimeInterval = array[safe: 1]?.double(),Date(timeIntervalSince1970: dateTimeInterval).isInToday{
                isInToday = true
            }
            
//            userid == self  && 时间是今天 && 次数为3 不在调用接口
            if array.first == user.accid && isInToday && times > 3 {
                
                return
            }
        }
        
        if isInToday {
            times += 1
        }
//        不是今天第一次request  times = 1
        else{
            times = 1
        }
        

        Defaults.setValue(user.accid + "@" + Date().unixTimestamp.int.string + "@" + times.string, forKey: key)
        
        Defaults.synchronize()
        
        JVrequest(PH.requestAutoChatGiftApi) { (json) in
            
            print("女生自动发礼物接口调试：",json)
            
        } failure: { (code, err) in
            
            print("女生自动发礼物接口调试错误：",code,"---Err:",err)
            
        }
    }
}
